package com.ykt.mall.ctrl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.ykt.mall.content.Content;
import com.ykt.model.Product;
import com.ykt.model.ProductType;
import com.ykt.model.Shop;

@ControllerBind(controllerKey = "/menu")
public class MenuCtrl extends Controller{
	
	public void view() {
		
		setAttr("shopPo", Shop.dao.findById(getParaToInt()));
		
		setAttr("productTypeList", ProductType.dao.find("select * from product_type where pid=0 || pid =null  order by id desc;"));
		
		List<ProductType> list = ProductType.dao.find("select * from product_type where pid=0 || pid =null  order by id desc;");
		Map<Integer,List<Record>> map = new HashMap<Integer,List<Record>>();
		
		for(ProductType ptype : list){
			int id = ptype.getInt("id");
			List<Record> pList = Db.find(" select id,name,price from product where product_type_id = ? order by id desc",id);
			map.put(id, pList);
		}
		//System.out.println(map.get(30));
		setAttr("map",map);
	}
	/**
	 * 所有类别
	 * http://m.caichongwang.com/categories.aspx
	 */
	public void categories(){
		Integer  shopId = getParaToInt();
		
		Shop shop = getSessionAttr(Content.SESSION_SHOP);
		if(shopId==null) shopId=shop.getInt("id");//测试数据
		List<ProductType> typeList =ProductType.dao.findByPid(0,shopId);//获得顶层类型
		setAttr("list", typeList);
	}
}
